<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="总金额" prop="totalAmount">
        <el-input v-model="queryParams.totalAmount" placeholder="请输入总金额" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input v-model="queryParams.deptId" placeholder="请输入部门ID" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类别" clearable size="small">
          <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="发生时间" prop="findTime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.findTime" type="date"
          value-format="yyyy-MM-dd" placeholder="选择发生时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发生机构" prop="findDeptId">
        <el-input v-model="queryParams.findDeptId" placeholder="请输入发生机构" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['grc:lossevent:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['grc:lossevent:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['grc:lossevent:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['grc:lossevent:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="losseventList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
    <!--  <el-table-column label="id" align="center" prop="id" /> -->

      <el-table-column label="编号" align="center" prop="code"  width="150"  >
        <template slot-scope="scope">
              <el-link type="primary" @click="handleDetail(scope.row)" >{{ scope.row.code}}</el-link>
        </template>
      </el-table-column>

      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="部门ID" align="center" prop="deptId" />

      <el-table-column label="类别" align="center" prop="type" :formatter="typeFormat" />

      <el-table-column label="发生时间" align="center" prop="findTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.findTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发生机构" align="center" prop="findDeptId" />
       <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />

      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-promotion" @click="handlePoces(scope.row)" v-hasPermi="['activiti:pocess:edit']">提交</el-button>
          <el-button size="mini" type="text" icon="el-icon-share" @click="historicActivity (scope.row)" v-hasPermi="['workflow:leave:edit']">追溯 </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />



    <!-- 添加或修改损失事件对话框  :fullscreen="true" -->
    <el-dialog :title="detailDialog.title" :visible.sync="detailDialog.open" width="1200px" append-to-body :before-close="handleClose"

   

      >
      <LosseventDetailTabs :detailDialog.sync="detailDialog" @doRefresh="getList"> </LosseventDetailTabs>
    </el-dialog>

    <el-dialog :title="historicDialog.title" :visible.sync="historicDialog.open" width="1000px" append-to-body>
      <div class="chart-wrapper">
        <pocessHistoric :detailDialog.sync="historicDialog">
        </pocessHistoric>
      </div>
    </el-dialog>


  </div>
</template>

<script>

  import pocessHistoric from "@/views/activiti/pocess/pocessHistoric.vue";
  import LosseventDetailTabs from "./LosseventDetailTabs.vue";



  import {
    listLossevent,
    getLossevent,
    delLossevent,
    addLossevent,
    updateLossevent,
    exportLossevent
  } from "@/api/grc/lossevent";


  export default {
    name: "Lossevent",
    components: {
      pocessHistoric,
      LosseventDetailTabs,

    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 损失事件表格数据
        losseventList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,

        // 弹出层标题
        detailDialog: {
          //弹框标题
          title: "",

          //业务id
          id: "",
          open: false,
          //是否是详情页 true：是
          isDisabled: true,



          //工作流控制
          //是否弹出工作流
          hasPoces: false,
          befinitionKey: "lossevent_6",
        //  processDefinitionId: "lossevent_6:19:d25fe605-5712-11eb-bf81-b0359fadfbcass",
          vars: {
            acti_num: 4,
            title: "工作流标题",
            code: "",
            componentName: "LosseventDetail",
          },
        },

        //流程追溯
        historicDialog: {
          //  流程实例  roInstanceId
          id: "",
          title: "",
          open: false,
          // 业务id
          businessId: "",
          befinitionKey: "",
        },


        // 状态字典
        statusOptions: [],
        // 类别字典
        typeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          code: null,
          name: null,
          totalAmount: null,
          deptId: null,
          status: null,
          type: null,
          findTime: null,
          findDeptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("T21_flow_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("t21_ldc_lossevent_type").then(response => {
        this.typeOptions = response.data;
      });
    },

    watch: {
      /* 监听打开状态 */
      /* 'detailDialog.open': function(newOpen, oldOpen) {
        //显示则刷新
        if (newOpen) {
          console.log("父页面监听子页面打开")
          this.detailDialog.tabs.activeName = 'first'
        }
      }, */
    },

    methods: {

      /** 关闭弹框判断 */
       handleClose(done) {
         if(!this.detailDialog.isDisabled){
           this.$confirm('确认关闭？')
               .then(_ => {
                 done();
               })
               .catch(_ => {});
         }else{
           done();
         }
      },
      /** 查询损失事件列表 */
      getList() {
        this.loading = true;
        listLossevent(this.queryParams).then(response => {
          this.losseventList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 类别字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.type);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          code: null,
          name: null,
          totalAmount: null,
          deptId: null,
          status: "0",
          type: null,
          findTime: null,
          findDeptId: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {

        this.detailDialog.id = "";
        this.detailDialog.open = true;
        this.detailDialog.isDisabled = false;
        this.detailDialog.hasPoces = false;
        this.detailDialog.title = "添加损失事件";

      },

      /** 历史 */
      historicActivity(row) {
        const businessId = row.id

        this.historicDialog.open = true;
        this.historicDialog.title = "流程追溯";
        this.historicDialog.businessId = businessId;
        this.historicDialog.befinitionKey = this.detailDialog.befinitionKey
      },

      /** 工作流操作 */
      handlePoces(row) {
        const pocessId = row.id
        this.detailDialog.id = pocessId;
        this.detailDialog.open = true;
        this.detailDialog.isDisabled = true;
        this.detailDialog.hasPoces = true;
        this.detailDialog.title = "提交审批流程";
        this.detailDialog.vars.code = row.code;
      },

      /** 详情按钮操作 */
      handleDetail(row) {
        const pocessId = row.id || this.ids[0]
        this.detailDialog.id = pocessId;

        this.detailDialog.open = true;
        this.detailDialog.isDisabled = true;
        this.detailDialog.hasPoces = false;
        this.detailDialog.title = "详情";

      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        const pocessId = row.id || this.ids[0]
        this.detailDialog.id = pocessId;
        this.detailDialog.open = true;
        this.detailDialog.isDisabled = false;
        this.detailDialog.hasPoces = false;
        this.detailDialog.title = "修改";
      },

      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除损失事件编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLossevent(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有损失事件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportLossevent(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
